home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-08-03 | 3.8 KB | 159 lines | [TEXT/PJMM] |
- unit StringUtilities;
- interface
- uses
- Sane;
- const
- CR = char(13);
- tab = char(9);
- comma = char(44);
-
- function PtrToString (var theStr: Str255): ptr;
- procedure HandleToStr255 (aHandle: handle;
- var theString: str255;
- var error: boolean);
- procedure AppendString (theText: handle;
- theString: str255;
- var error: boolean);
- function IsNumber (theText: Str255;
- IsInteger: boolean): boolean;
- function StringToReal (number: Str255): extended;
- function RealToFixed (number: extended;
- decimals: integer): Str255;
- function RealToFormat (number: extended;
- theForm: DecForm): Str255;
- function RealToString (number: extended;
- sigFigs: integer): Str255;
- implementation
- {*******************************************************}
- function PtrToString (var theStr: Str255): ptr;
- begin
- PtrToString := ptr(longint(@theStr) + 1);
- end;
- {*********************************}
- procedure HandleToStr255;
- var
- i, len: integer;
- begin
- if not error then
- begin
- theString := '';
- len := GetHandleSize(aHandle);
- if len > 255 then
- len := 255;
- for i := 1 to len do
- theString := concat(theString, charsHandle(aHandle)^^[i - 1]);
- end;
- end;
- {*********************************}
- procedure AppendString;
- var
- err: OSErr;
- begin
- if not error then
- begin
- err := PtrAndHand(PtrToString(theString), theText, length(theString));
- error := (err <> noErr);
- end;
- end;
- {*********************************}
- function OrderOfMagnitude (number: extended): integer;
- {this is the order of magnitude which puts a number in scientific notation}
- var
- magnitude: extended;
- begin
- if number <> 0 then
- begin
- magnitude := ln(abs(number)) / ln(10);
- if magnitude >= 0 then
- magnitude := trunc(magnitude)
- else if (trunc(magnitude) <> magnitude) then
- magnitude := trunc(magnitude) - 1;
- end
- else
- magnitude := 1;
- OrderOfMagnitude := trunc(magnitude);
- end;
- {**************************************************}
- function IsNumber (theText: Str255;
- IsInteger: boolean): boolean;
- var
- oneExp, oneDec, badText: boolean;
- i: integer;
- begin
- oneExp := false;
- oneDec := false;
- BadText := false;
- for i := 1 to length(theText) do
- case theText[i] of
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',':
- ;{do nothing}
- 'e', 'E':
- if oneExp then
- badText := true
- else
- oneExp := true;
- '.':
- if oneDec then
- badText := true
- else
- oneDec := true;
- '-', '–':
- if not oneExp then
- BadText := (i > 1);
- otherwise
- BadText := true;
- end;{case}
-
- if (oneExp or oneDec) and IsInteger then
- badText := true;
-
- IsNumber := not BadText;
- end;
- {*************************************************************************}
- function StringToReal;
- begin
- StringToReal := Str2Num(number);
- end;
- {*****************************************}
- function RealToFixed;
- var
- order: integer;
- tempStr: DecStr;
- theForm: DecForm;
- begin
- order := OrderOfMagnitude(number);
- theForm.digits := decimals;
- theForm.style := FixedDecimal;
- Num2Str(theForm, number, tempStr);
- RealToFixed := tempStr;
- end;
- {*****************************************}
- function RealToFormat;
- var
- tempStr: DecStr;
- begin
- Num2Str(theForm, number, tempStr);
- RealToFormat := tempStr;
- end;
- {*****************************************}
- function RealToString;
- var
- order: integer;
- tempStr: DecStr;
- theForm: DecForm;
- begin
- order := OrderOfMagnitude(number);
- if (order > sigFigs) or (order < -1) then
- begin
- theForm.style := FloatDecimal;
- theForm.digits := sigFigs - 1;
- end
- else
- begin
- theForm.style := FixedDecimal;
- theForm.Digits := sigFigs - order - 1;
- end;
- Num2Str(theForm, number, tempStr);
- RealToString := tempStr;
- end;
- end.